////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2008 Chris Colinsky
// All Rights Reserved
//
// version	0.4
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

import sugarcookie.validators.ValidationResult;

class sugarcookie.validators.NumberValidator {
	public static var allowNegative:Boolean = true;
	public static var maxValue:Number = null;
	public static var minValue:Number = null;
	
	public static function isRealValue(value:Object):Boolean {
		if (isNaN(value) || value==null || value==undefined) return false;
		else return true;
	}
	
	public static function validate(value:Object):ValidationResult {
		var vResult:ValidationResult = new ValidationResult();
		vResult.isError = false;
		if (isRealValue(value)) {
			if (!allowNegitive && Number(value)<0 || maxValue!=null && Number(value)>maxValue || minValue!=null && Number(value)<minValue) vResult.isError = true;
			vResult.errorMessage = vResult.isError?"Value passed could possibly be out of range or negitive, depending on vaidator settings":"";
			return vResult;
		} else {
			vResult.errorMessage = "Value passed was not a real number.";
			vResult.isError = false;
			return vResult;
		}
	}
}